# encoding = utf-8


data = [(3, 3, 1), (4, 3, 1), (1, 1, -1)]


# 感知机原始问题（线性可分）
class original_perceptron:
    w0 = 0
    w1 = 0
    b = 0
    n = 1

    # 判断是否有无分类点
    def is_continue(self):
        for x, y, z in data:
            temp = self.w0 * x + self.w1 * y + self.b
            if (temp >= 0 and z == -1):
                return True
            elif (temp <= 0 and z == 1):
                return True
        return False

    def change(self, x, y, z):
        self.w0 = self.w0 + z * x * self.n
        self.w1 = self.w1 + z * y * self.n
        self.b = self.b + z * self.n
        print(self.w0)
        print(self.w1)
        print(self.b)

    def run(self):
        while self.is_continue():
            for x, y, z in data:
                temp = self.w0 * x + self.w1 * y + self.b
                if (temp >= 0 and z == -1):
                    self.change(x, y, z)
                elif (temp <= 0 and z == 1):
                    self.change(x, y, z)


# 感知机对偶问题（线性可分）
class duality_perceptron:
    # 各个点改变次数
    count = [0, 0, 0]

    w0 = 0
    w1 = 0
    b = 0
    n = 1

    def __init__(self):

        pass

    def get_params(self):
        for index, item in enumerate(data):
            self.w0 = self.w0 + self.count[index] * self.n * item[2] * item[0]
            self.w1 = self.w1 + self.count[index] * self.n * item[2] * item[1]
            self.b = self.b + self.count[index] * self.n * item[2]

    # 判断是否有无分类点
    def is_continue(self):
        for index, item in enumerate(data):
            temp = self.w0 * item[0] + self.w1 * item[1] + self.b
            if (temp >= 0 and item[2] == -1):
                return True
            elif (temp <= 0 and item[2] == 1):
                return True
        return False

    def change(self):
        self.get_params()
        print(self.count)

    def run(self):
        while self.is_continue():

            for index, item in enumerate(data):
                temp = self.w0 * item[0] + self.w1 * item[1] + self.b
                if (temp >= 0 and item[2] == -1):
                    self.count[index] = self.count[index] + 1
                    self.change()
                elif (temp <= 0 and item[2] == 1):
                    self.count[index] = self.count[index] + 1
                    self.change()


if __name__ == '__main__':
    duality_perceptron().run()
